import {Injectable, Injector} from '@angular/core';
import 'rxjs/add/operator/map';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from "@angular/common/http";
import {Observable} from "rxjs/Observable";
import {User} from "../user/user";

/*
  Generated class for the JwtInterceptorProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@Injectable()
export class JwtInterceptorProvider implements HttpInterceptor {
  private user: User;

  constructor(private inj: Injector) {
    console.log('Hello JwtInterceptorProvider Provider');
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    this.user = this.inj.get(User);
    return next.handle(req).map(event => {
        if (event instanceof HttpResponse) {
          if (event.status === 200) {

          }
          return event;
        }
      }
    );
  }

}
